var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      mypoint:options.myPoint
    })
  },
  onReady: function () {
    var that = this;
    this.dialog = this.selectComponent("#dialog");
  },
  showDialog(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      delId:id
    });
    this.dialog.showDialog();
  },
  onShow: function () {
    var that = this;
    var id =wx.getStorageSync("userId");
    //获取手机高度
    that.setData({
      navH: app.globalData.navHeight
    })
    wx.request({
      url: app.globalData.url + 'getUserByUserId',
      data: {
        userId: id,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var user = res.data.data;
        if(user){
          if (user.sex != 0) {
            user.sex = (user.sex === 1 ? '男' : '女');
          } else {
            user.sex = '';
          }
          // 更新数据  
          that.setData({
            user: user
          });
        }
        
      },
      fail: function (err) {

      },
    })
  },
  bindProvinceNameChange: function (e) {
    var that = this;
    console.log('picker province 发生选择改变，携带值为', e.detail.value);
    var val = e.detail.value;
    var arr = e.target.dataset.pri[val];
    province = arr;
    console.log(province);
    that.getCityArr(arr, val); //获取地级市数据
    this.setData({
      provinceIndex: e.detail.value,
      cityIndex: 0,
      countyIndex: 0,
    })
  },

  bindCityNameChange: function (e) {
    var that = this;
    console.log('picker city 发生选择改变，携带值为', e.detail.value);
    var val = e.detail.value;
    var arr = e.target.dataset.city[val];
    that.getCountyInfo(arr, val); //获取区县数据
    city = arr;
    this.setData({
      cityIndex: e.detail.value,
      countyIndex: 0,
    })
  },

  bindCountyNameChange: function (e) {
    var that = this;
    console.log('picker county 发生选择改变，携带值为', e.detail.value);
    var arr = e.target.dataset.cou[val];
    county = arr;
    this.setData({
      countyIndex: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {
  //   this.onLoad();
  // },
  addAddress: function (e) {
    wx.navigateTo({ 
      url: '../address/address'
    });
  },
  upUserInfo: function (e) {
    var address = JSON.stringify(e.currentTarget.dataset.index);
    wx.navigateTo({
      url: '../upUserInfo/upUserInfo'
    });
  },
  setDefautAddress : function(e){
      var that = this;
      wx.request({
        url: app.globalData.url + 'setDefaultAddress',
        data: {
          addrId: e.currentTarget.dataset.id,
          userId:wx.getStorageSync("userId")
        },
        success: function(res) {
          that.onShow();
        },
        fail: function(res) {

        },
      })
  },
  /* 删除item */
  delAddress: function (addressId) {
    var that = this;
    var addrId = that.data.delId;
    console.log(addrId);
    wx.request({
      url: app.globalData.url + 'deUserAddress',
      data: {
        addrId: addrId,
        userId: wx.getStorageSync("userId")
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.onShow();
      },
      fail: function (err) {

      },
    })
  
  },
  dianji:function(e){
    let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。

    let prevPage = pages[pages.length - 2];
    console.info(e.currentTarget.dataset.username);
    console.info(e.currentTarget.dataset.telephone);
    console.info(e.currentTarget.dataset.wholeaddress);
    console.info(e.currentTarget.dataset.adid);

    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。

      username: e.currentTarget.dataset.username,

      telephone: e.currentTarget.dataset.telephone,

      wholeAddress: e.currentTarget.dataset.wholeaddress,

      addrId: e.currentTarget.dataset.adid,
      addrid: e.currentTarget.dataset.adid,
      tuan :2
    })
    wx.navigateBack({
      data:1
    })
  },

  _cancelEvent() {
    console.log('你点击了取消');
    this.dialog.hideDialog();
  },
  //确认事件
  _confirmEvent() {
    var that = this;
    console.log('你点击了确定');
    that.delAddress();
    this.dialog.hideDialog();
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  setDefault:function(e){
    var that = this;
    wx.request({
      url: app.globalData.url + 'setDefaultAddressByAddressId',
      data: {
        addrId: e.currentTarget.dataset.id,
        userId: wx.getStorageSync("userId")
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.onShow();
      },
      fail: function (err) {

      },
    })
  }
})